<?php
/**
 *
 * 作    者：安德玛
 * 版本控制：
			2021-02-08------新增用户提现限制
 * 功能说明：用户提现控制器。
 *
 **/
namespace Api\Controller;

class TixianController extends ComController
{
    /**
     * 提现页面
     */
    public function index()
    {
        // 用户可提现金额
        $data['money'] = $this->user['money'];
		$k = "TIXIAN_PERCENT";
		$var = M('setting')->where("k='$k'")->find();
        $data['percent'] = $var['v'];
        // 用户默认收款账户
        $account = M('user_payment')->where(array('uid'=>$this->user['id']))->find();
        if($account['type'] == 1){
            $data['account'] = $account['card_no'];
        }else if($account['type'] == 2){
            $data['account'] = $account['wechat_no'];
        }else{
            $data['account'] = $account['alipay_no'];
        }
        $data['type'] = $account['type'];
        $this->res['data'] = $data;
        $this->response($this->res,'json');
    }
    
    /**
     * 提现提交
     */
    public function submit()
    {
        if(!IS_POST){
            $this->res['code'] = 100;
            $this->res['msg'] = '请用post方法请求接口';
            $this->response($this->res,'json');
        }
        $payment = M('user_payment')->where(array('uid'=>$this->user['id']))->find();
        if(empty($payment)){
            $this->res['code'] = 101;
            $this->res['msg'] = '请先设置收款账户信息，否提现无法完成！';
            $this->response($this->res,'json');
        }
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        $data['tixian_money'] = $json->tixian_money;
        $data['account_type'] = $json->type;
        if(!$data['tixian_money']){
            $this->res['code'] = 102;
            $this->res['msg'] = '提现金额为空';
            $this->response($this->res,'json');
        }
        
        // if(floatval($data['tixian_money']) == 0 ){
        //     $this->res['code'] = 102;
        //     $this->res['msg'] = '提现金额不合法';
        //     $this->response($this->res,'json');
        // }
        
        if(!$data['account_type']){
            $this->res['code'] = 102;
            $this->res['msg'] = 'type为空';
            $this->response($this->res,'json');
        }
        /**
        if(floatval($this->user['money']) < 100){
            $this->res['code'] = 103;
            $this->res['msg'] = '累计金额小于100元';
            $this->response($this->res,'json');
        }*/
        
        // if(floatval($data['tixian_money']) > floatval($this->user['money'])){
        //     $this->res['code'] = 104;
        //     $this->res['msg'] = '提现金额不能大于实际金额';
        //     $this->response($this->res,'json');
        // }
		$k = "TIXIAN_PERCENT";
		$var = M('setting')->where("k='$k'")->find();
        $percent = $var['v'];
        
		$final_money1 = round(floatval($data['tixian_money']) - floatval($data['tixian_money']) * $percent,2);
		$final_money = floatval($final_money1)-1;
		$data['final_money'] = $final_money;
        $data['uid'] = $this->user['id'];
        $data['t'] = time();
        $data['status'] = 1;
        
        $model1 = M('tixian');
        $model2 = M('user');
        
        $model1->startTrans();
        
        $res1 = $model1->data($data)->add();
        $res2 = $model2->where('id='.$this->user['id'])->setDec('money',$data['tixian_money']);
        if($res1 && $res2){
            $model1->commit();
            $this->res['msg'] = '提交成功，请等待管理员审核！';
            $this->response($this->res,'json');
        }else{
            $model1->rollback();
            $this->res['code'] = 500;
            $this->res['msg'] = '提交失败，请检查输入表单！';
            $this->response($this->res,'json');
        }
    }
    
    /**
     * 提现记录
     */
    public function tixianList()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : '10';
        $m = M('tixian');
        $where['uid'] = $this->user['id'];
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $m->where($where)->count();
        $list = $m->where($where)->order('t desc')->limit($offset . ',' . $pagesize)->select();
        foreach ($list as $k=>&$v){
            $list[$k]['t'] = date('Y-m-d H:i:s',$v['t']);
            //获取收款账户
            $account = M('user_payment')->where(array('uid'=>$this->user['id']))->find();
            if($v['account_type'] == 1){
                $list[$k]['account'] = $account['card_no'];
            }else if($v['account_type'] == 2){
                $list[$k]['account'] = $account['wechat_no'];
            }else{
                $list[$k]['account'] = $account['alipay_no'];
            }
        }
        $page = new \Think\Page($count, $pagesize);
        $page->show();
        $page_arr = array(
            'totalCount'=>intval($count),
            'pageSize'=>$pagesize,
            'totalPage'=>$page->totalPages,
            'currPage'=>intval($p)
        );
        $this->res['data']['page'] = $page_arr;
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
    
    /**
     * 获取提现进度
     */
    public function getStep()
    {
        $id = I('get.id','','htmlentities');
        if(!$id){
            $this->res['code'] = 101;
            $this->res['msg'] = 'id为空！';
            $this->response($this->res,'json');
        }
        $tixian_info = M('tixian')->where('id='.$id)->field('status')->find();
        $this->res['data'] = $tixian_info;
        $this->response($this->res,'json');
    }
	/* 提现次数查询 */
	public function queryTixian()
	{
	$tixian_num = M('tixian')->where(array('uid'=>$this->user['id'],'status'=>2))->count();
	
	$this->res['data'] = $tixian_num;
	$this->response($this->res,'json');
	}
}